package com.jfcloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jfcloud.common.http.RespData;
import com.jfcloud.common.http.RespDataT;
import com.jfcloud.common.utils.MapUtil;
import com.jfcloud.common.utils.StringUtil;
import com.jfcloud.mall.product.domain.JfcloudProductSku;
import com.jfcloud.mall.product.domain.dto.JfcloudStorage;
import com.jfcloud.mall.product.domain.dto.ProductVo;
import com.jfcloud.mall.product.domain.dto.StorageQueryForm;
import com.jfcloud.mall.product.mapper.ProductMapper;
import com.jfcloud.mall.product.persistent.JfcloudProductSkuService;
import com.jfcloud.mall.product.rpc.feign.StorageServiceClient;
import com.jfcloud.mall.product.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Autowired
	private JfcloudProductSkuService  jfcloudProductSkuService;
	@Autowired
	private StorageServiceClient storageService;

	@Autowired
	private ProductMapper productMapper;


	@Override
	public boolean queryStorage(IPage<ProductVo> datas) {
		try {
			List<Long> ids = new ArrayList<Long>();
			datas.getRecords().forEach(data -> {
				ids.add(data.getId());
			});
			StorageQueryForm query = new StorageQueryForm();
			Long[] idArr = {};
			RespData rd = storageService.getStorages(ids.toArray(idArr));
			if (RespData.Success==rd.getStatus()) {
				List<Map<String, Object>> rDatas = (List<Map<String, Object>>) rd.getDatas();
				for (Map m : rDatas) {
					Long productId = MapUtil.getLong("productId",m);
					Long num = MapUtil.getLong("storageNum",m);
					datas.getRecords().forEach(data -> {
						if (data.getId().equals(productId)) {
							data.setStorage(num);
						}
					});
				}
				return true;
			} else {
				LOGGER.error("" + rd.getMeta());
				return false;
			}
		}catch (Exception e){
			LOGGER.error(e.getMessage(),e);
			return false;
		}
	}

	@Override
	public boolean queryStorageByEntity(IPage<ProductVo> datas) {
		try {
			List<Long> ids = new ArrayList<Long>();
			datas.getRecords().forEach(data -> {
				ids.add(data.getId());
			});
			Long[] idArr = {};
			RespDataT<List<JfcloudStorage>> rd = storageService.getStoragesByEntity(ids.toArray(idArr));
			if (RespDataT.Success==rd.getStatus()) {
				List<JfcloudStorage> rDatas =  rd.getDatas();
				for (JfcloudStorage m : rDatas) {
					Long productId = m.getProductId();
					Long num = m.getStorageNum();
					datas.getRecords().forEach(data -> {
						if (data.getId().equals(productId)) {
							data.setStorage(num);
						}
					});
				}
				return true;
			} else {
				LOGGER.error("" + rd.getMeta());
				return false;
			}
		}catch (Exception e){
			LOGGER.error(e.getMessage(),e);
			return false;
		}
	}

	@Override
	public Map<Long,JfcloudProductSku> getProductSkus(String ids){
		Map<Long,JfcloudProductSku> mp = new HashMap<Long,JfcloudProductSku>();
		if(StringUtil.isNotEmpty(ids)) {
			List<Long> skuIds = StringUtil.fromSplitStr(ids, ",");
			List<JfcloudProductSku> skus = (List<JfcloudProductSku>) jfcloudProductSkuService.listByIds(skuIds);
			for(JfcloudProductSku s:skus) {
				mp.put(s.getId(), s);
			}
		}
		
		return mp;
	}
}
